package cn.janyd.jmall.user.manager;

import cn.hutool.core.lang.UUID;
import cn.janyd.common.core.enums.CommonStatusEnum;
import cn.janyd.common.core.util.EncryptUtil;
import cn.janyd.common.core.util.ServiceExceptionUtil;
import cn.janyd.jmall.user.convert.UserConverter;
import cn.janyd.jmall.user.dal.dataobject.user.UserDO;
import cn.janyd.jmall.user.dal.mapper.user.UserMapper;
import cn.janyd.jmall.user.manager.bo.UserCreateBO;
import cn.janyd.jmall.user.manager.bo.UserUpdateBO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import static cn.janyd.jmall.user.constants.UserErrorCode.USER_NOT_EXIST;

/**
 * @author janyd
 * @since 2020/8/10 16:43
 */
@Service
public class UserManager {

    private final UserMapper userMapper;

    public UserManager(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    /**
     * 根据手机号码获取用户信息
     *
     * @param mobile 手机号码
     * @return 用户信息
     */
    public UserDO getUser(String mobile) {
        return userMapper.selectOne(Wrappers.<UserDO>lambdaQuery()
                .eq(UserDO::getPhone, mobile));
    }

    /**
     * 创建用户
     *
     * @param createBO 用户基础信息
     * @return 用户信息
     */
    public UserDO createUser(UserCreateBO createBO) {
        UserDO userDO = UserConverter.INSTANCE.convert(createBO)
                .setStatus(CommonStatusEnum.ENABLE);

        String salt = genPasswordSalt();
        String password = createBO.getPassword();

        if (StringUtils.isBlank(password)) {
            password = genPassword();
        }
        password = encodePassword(password, salt);
        userDO.setPassword(password)
                .setSalt(salt);
        userMapper.insert(userDO);
        return userDO;
    }

    /**
     * 更新用户信息
     *
     * @param userUpdate 用户信息
     */
    public void updateUser(UserUpdateBO userUpdate) {
        UserDO userDO = userMapper.selectById(userUpdate.getId());

        if (userDO == null) {
            throw ServiceExceptionUtil.exception(USER_NOT_EXIST);
        }


        if (StringUtils.isNotBlank(userUpdate.getNickname())) {
            userDO.setNickname(userUpdate.getNickname());
        }

        if (StringUtils.isNotBlank(userUpdate.getAvatar())) {
            userDO.setAvatar(userUpdate.getAvatar());
        }

        if (StringUtils.isNotBlank(userUpdate.getPhone())) {
            userDO.setPhone(userUpdate.getPhone());
        }

        if (userUpdate.getStatus() != null) {
            userDO.setStatus(userUpdate.getStatus());
        }

        if (StringUtils.isNotBlank(userUpdate.getPassword())) {
            String salt = genPasswordSalt();
            String password = encodePassword(userUpdate.getPassword(), salt);
            userDO.setPassword(password)
                    .setSalt(salt);
        }

        userMapper.updateById(userDO);
    }

    private String genPasswordSalt() {
        return EncryptUtil.genBcryptSalt();
    }

    private String genPassword() {
        return UUID.fastUUID().toString(true);
    }

    private String encodePassword(String password, String salt) {
        return EncryptUtil.encrypt(password, salt);
    }
}
